﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using WarehouseManagement.DataAccess;

namespace WarehouseManagement.BusinessLogic.DTO
{
    public partial class MerchandiseDTO : INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private int _Id;

        private string _MerchandiseName;

        private string _Unit;

        private string _TradeMark;

        private string _Madein;

        private int? _CategoryId;



        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnIdChanging(int value);
        partial void OnIdChanged();
        partial void OnMerchandiseNameChanging(string value);
        partial void OnMerchandiseNameChanged();
        partial void OnUnitChanging(string value);
        partial void OnUnitChanged();
        partial void OnTradeMarkChanging(string value);
        partial void OnTradeMarkChanged();
        partial void OnMadeinChanging(string value);
        partial void OnMadeinChanged();
        partial void OnCategoryIdChanging(int? value);
        partial void OnCategoryIdChanged();
        #endregion

        public MerchandiseDTO()
        {

            OnCreated();
        }

        public int Id
        {
            get
            {
                return this._Id;
            }
            set
            {
                if ((this._Id != value))
                {
                    this.OnIdChanging(value);
                    this.SendPropertyChanging();
                    this._Id = value;
                    this.SendPropertyChanged("Id");
                    this.OnIdChanged();
                }
            }
        }

        public string MerchandiseName
        {
            get
            {
                return this._MerchandiseName;
            }
            set
            {
                if ((this._MerchandiseName != value))
                {
                    this.OnMerchandiseNameChanging(value);
                    this.SendPropertyChanging();
                    this._MerchandiseName = value;
                    this.SendPropertyChanged("MerchandiseName");
                    this.OnMerchandiseNameChanged();
                }
            }
        }

        public string Unit
        {
            get
            {
                return this._Unit;
            }
            set
            {
                if ((this._Unit != value))
                {
                    this.OnUnitChanging(value);
                    this.SendPropertyChanging();
                    this._Unit = value;
                    this.SendPropertyChanged("Unit");
                    this.OnUnitChanged();
                }
            }
        }

        public string TradeMark
        {
            get
            {
                return this._TradeMark;
            }
            set
            {
                if ((this._TradeMark != value))
                {
                    this.OnTradeMarkChanging(value);
                    this.SendPropertyChanging();
                    this._TradeMark = value;
                    this.SendPropertyChanged("TradeMark");
                    this.OnTradeMarkChanged();
                }
            }
        }

        public string Madein
        {
            get
            {
                return this._Madein;
            }
            set
            {
                if ((this._Madein != value))
                {
                    this.OnMadeinChanging(value);
                    this.SendPropertyChanging();
                    this._Madein = value;
                    this.SendPropertyChanged("Madein");
                    this.OnMadeinChanged();
                }
            }
        }

        public int? CategoryId
        {
            get
            {
                return this._CategoryId;
            }
            set
            {
                if ((this._CategoryId != value))
                {

                    this.OnCategoryIdChanging(value);
                    this.SendPropertyChanging();
                    this._CategoryId = value;
                    this.SendPropertyChanged("CategoryId");
                    this.OnCategoryIdChanged();
                }
            }
        }

        public string CategoryName { get; set; }




        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }




    }

    public static class MerchandiseDTOExtensions
    {
        public static MerchandiseDTO ToDTO(this Merchandise entity)
        {
            return new MerchandiseDTO()
            {
                CategoryId = entity.CategoryId,
                Id = entity.Id,
                Madein = entity.Madein,
                MerchandiseName = entity.MerchandiseName,
                TradeMark = entity.TradeMark,
                Unit = entity.Unit,
                CategoryName = entity.MerchandiseCategory == null ? "Chưa phân nhóm" 
                : entity.MerchandiseCategory.MerchandiseCategoryName
            };
        }

        public static Merchandise ToEntity(this MerchandiseDTO dto)
        {
            return new Merchandise()
            {
                CategoryId = dto.CategoryId,
                Id = dto.Id,
                Madein = dto.Madein,
                MerchandiseName = dto.MerchandiseName,
                TradeMark = dto.TradeMark,
                Unit = dto.Unit,

            };
        }
    }
}
